package com.kuqi.controller;

import com.kuqi.domain.Role;
import com.kuqi.domain.User;
import com.kuqi.service.RoleService;
import com.kuqi.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;

/**
 * @author:kuqi
 * @createTime:2021/10/16 15:36
 * @description:
 */

@Controller  //Spring框架，实例化Bean标识
@RequestMapping("/user")
public class UserController {

    @Autowired  //Spring框架注释，依赖注入
    private UserService userService;

    @Autowired
    private RoleService roleService;

    @RequestMapping("/delete.do/{userId}")
    public String deleteUser(@PathVariable("userId") Long userId){  //Restful风格的请求地址
        userService.deleteUser(userId);
        return "redirect:/user/list.do";
    }

    @RequestMapping("/save.do")
    public String save(User user,long[] roleIds){
        userService.save(user,roleIds);
        return "redirect:/user/list.do";
    }

    @RequestMapping("/saveUi.do")
    public ModelAndView saveUi(){
        ModelAndView modelAndView = new ModelAndView();  //SpringMVC提供的页面转发对象
        List<Role> roleList = roleService.getRoleList();
        modelAndView.addObject("roleList",roleList);
        modelAndView.setViewName("user-add");
        return modelAndView;
    }

    @RequestMapping("/list.do")
    public ModelAndView getUserList(){
        ModelAndView modelAndView = new ModelAndView();  //SpringMVC提供的页面转发对象
        List<User> userList = userService.getUserList();
        modelAndView.addObject("userList",userList);
        modelAndView.setViewName("user-list");
        return modelAndView;
    }

}
